(ns acst.routes.blog
  (:use acst.config.error-config
        acst.config.success-config
        acst.utils.core)
  (:require [noir.util.crypt :as crypt]
            [noir.session :as session]
            [noir.validation :as vali]
            [selmer.parser :as parser]
            [ring.middleware.anti-forgery :as af]
            [compojure.core :refer :all]
            [acst.models.users :refer :all]
            [clojure.data.json :as json]
            [acst.models.articles :refer :all]))

(defn get-all-articles [limit page]                         ;;显示所有文章
  (->(let [[int_limit int_page] (parse-to-int  limit page)]
       (find-all-articles int_limit int_page))
     (json/write-str)))

(defn add-article! [title content]                          ;;发表文章
  (login-require
    (-> (let [user_id (session/get "user_id")]
          (insert-article! user_id title content))
        (json/write-str))))

(defn remove-article! [article_id]                          ;;删除文章
  (login-require
    (-> (let [user_id (session/get "user_id") int_article_id (parse-to-int article_id)]
          (delete-article! int_article_id user_id))
        (json/write-str))))

(defn update-my-article! [article_id title content]            ;;更新文章
  (login-require
    (-> (let [user_id (session/get "user_id") int_article_id (parse-to-int article_id)]
          (acst.models.articles/update-article! user_id int_article_id title content))
        (json/write-str))))

(defn find-article [where-cluse limit page]
  (-> (let [[int_limit int_page] (parse-to-int limit page)]
        (find-articles where-cluse int_limit int_page))
      (json/write-str)))

(defn test-blog []
  (set-user-session! "acst" 1)
  ;(find-article 20 1 :blog_id "1")
  ;(find-article 20 1 :article_id "1")
  ;(find-article 20 1 :keyword "title")
  ;(get-all-articles 20 1)
  ;(remove-article! 1)
  ;(update-my-article! 2 "123" "123")
  ;(add-article! "title" "content")
  )

(defroutes blog-routes
           (GET "/blog" [limit page] (handler get-all-articles limit 20 page 1))
           (POST "/blog" [title content] (handler add-article! title nil content nil))
           (DELETE "/blog/:article_id" [article_id] (handler remove-article! article_id))
           (POST "/blog/:article_id" [article_id title content] (handler update-my-article! article_id nil title nil content nil))
           (GET "/blog/article_id/:article_id" [article_id limit page] (handler find-article {:article_id (first (parse-to-int article_id))} nil limit 20 page 1))
           (GET "/blog/blog_id/:blog_id" [blog_id limit page] (handler find-article {:blog_id  (first (parse-to-int blog_id))} nil limit 20 page 1))
           (GET "/blog/keyword/:keyword" [keyword limit page] (handler find-article {:keyword keyword} nil limit 20 page 1))
           (GET "/blog/blog_id/keyword/:blog_id/:keyword" [blog_id keyword limit page] (handler find-article {:blog_id (first (parse-to-int blog_id)) :keyword keyword} nil limit 20 page 1))
           (GET "/test/blog" [] (test-blog))
           )